﻿using Microsoft.Extensions.DependencyInjection;
using System;

namespace Demo
{
    public interface IStartup
    {
        IServiceProvider ConfigureServices(IServiceCollection services);

        void Configure(IApplicationBuilder app);
    }


    public class DelegateStartup : IStartup
    {
        private readonly IServiceProviderFactory<IServiceCollection> _factory;
        private Action<IApplicationBuilder> _configureApp;

        public DelegateStartup(IServiceProviderFactory<IServiceCollection> factory, Action<IApplicationBuilder> configureApp) 
        {

            _configureApp = configureApp;
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var builder = _factory.CreateBuilder(services);
            return _factory.CreateServiceProvider(builder);
        }

        public void Configure(IApplicationBuilder app) => _configureApp(app);
    }

}
